#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# emoticon_dialog.py.
#
#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
#
#  Author: Kenneth Rohde Christiansen <kenneth.christiansen@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import pango
import gobject

(
EMOTICON_COLUMN_PIXBUF,
EMOTICON_COLUMN_INFO,
EMOTICON_COLUMN_TAG,
) = range(3)

class EmoticonDialog(gtk.Dialog):

    def __init__(self, emoticons, buffer, parent=None):
        gtk.Dialog.__init__(self, parent=parent, flags=gtk.DIALOG_DESTROY_WITH_PARENT)

        self.resize(250, 250)

        self.buffer = buffer
        self.emoticons = emoticons

        self.set_title("Add emoticon...")
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)

        scrolledwindow = gtk.ScrolledWindow(None, None)
        self.vbox.pack_start(scrolledwindow, True, True, 0)
        scrolledwindow.set_border_width(6)
        scrolledwindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        scrolledwindow.set_shadow_type(gtk.SHADOW_IN)

        self.emoticon_treeview = gtk.TreeView()
        scrolledwindow.add(self.emoticon_treeview)

        pixbuf_renderer = gtk.CellRendererPixbuf()
        info_renderer = gtk.CellRendererText()
        info_renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
        info_renderer.set_property("ellipsize-set", True)

        emoticon_column = gtk.TreeViewColumn("Available Emoticons")
        self.emoticon_treeview.set_headers_visible(False)
        emoticon_column.pack_start(pixbuf_renderer, expand=False)
        emoticon_column.add_attribute(pixbuf_renderer, "pixbuf", EMOTICON_COLUMN_PIXBUF)
        emoticon_column.pack_start(info_renderer, expand=True)
        emoticon_column.add_attribute(info_renderer, "markup", EMOTICON_COLUMN_INFO)

        self.emoticon_model = gtk.TreeStore(gtk.gdk.Pixbuf, str, str)

        self.emoticon_treeview.set_model(self.emoticon_model)
        self.emoticon_treeview.set_property("rules-hint", True)
        self.emoticon_treeview.append_column(emoticon_column)
        self.emoticon_treeview.connect("row-activated", self.on_row_activated)

        self.action_area.set_layout(gtk.BUTTONBOX_END)

        self.close_button = gtk.Button(stock="gtk-close")
        self.close_button.connect('clicked', self.on_close_clicked)
        self.add_action_widget(self.close_button, gtk.RESPONSE_CLOSE)
        self.close_button.set_flags(gtk.CAN_DEFAULT)

        self.insert_button = gtk.Button()
        self.insert_button.connect('clicked', self.on_insert_clicked)
        self.add_action_widget(self.insert_button, gtk.RESPONSE_APPLY)
        self.insert_button.set_flags(gtk.CAN_DEFAULT)

        alignment = gtk.Alignment(0.5, 0.5, 0, 0)
        self.insert_button.add(alignment)

        insert_button_hbox = gtk.HBox(False, 2)
        alignment.add(insert_button_hbox)

        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)

        insert_button_hbox.pack_start(image, False, False, 0)

        insert_label = gtk.Label("Insert")
        insert_button_hbox.pack_start(insert_label, False, False, 0)

        self.add_emoticons()

        self.emoticon_treeview.expand_all()

        self.show_all()


    def on_insert_clicked(self, button):
        end_iter = self.buffer.get_end_iter()

        selection = self.emoticon_treeview.get_selection()
        model, selected_iter = selection.get_selected()

        pixbuf, tag = model.get(selected_iter, EMOTICON_COLUMN_PIXBUF, EMOTICON_COLUMN_TAG)

        if pixbuf != None:
            self.buffer.insert(end_iter, tag)


    def on_row_activated(self, treeview, path, view_column):
        self.on_insert_clicked(None)


    def on_close_clicked(self, button):
        self.destroy()


    def add_emoticons(self):
        emoticon_pixbufs = self.emoticons.get_emoticon_pixbufs()

        for group in self.emoticons.get_group_names():
            if group == "QQ" or group == "Yahoo":
                continue

            emoticon_infos = self.emoticons.get_emoticon_infos_from_group(group)

            if group == "default":
                group = "Default emoticons"

            parent = self.emoticon_model.append(None, [ None, "<b>" + group + "</b>", None])

            for info in emoticon_infos:
                tag_string = None

                if not info[1]:
                    continue

                for emoticon_tag in info[1]:
                    if tag_string == None:
                        tag_string = emoticon_tag
                    else:
                        tag_string = tag_string + " " + emoticon_tag

                tag_string = "   " + gobject.markup_escape_text(tag_string)

                self.emoticon_model.append(parent, \
                    [ emoticon_pixbufs[emoticon_tag], tag_string, info[1][0] ])
